<?php


namespace app\admin\controller;


use think\facade\View;
use think\Request;

class Config extends Base
{
    protected $ModelPath = 'Config';
    protected $model = null;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->con = "Config";
        $this->title = "配置";

        $module = $this->ModelPath;
        $this->model = new \app\admin\model\Config();

        View::assign(array(
            'module' => $module,
            'title' => $this->title,
            'con' => $this->con,
            'pageSize' => $this->pageSize
        ));
    }

    public function value(){
        $id = $_POST['id'];
        $status = $_POST['status'] == 'true'?'on':'off';
        $update = [
            'id'            =>  $id,
            'value'           =>  $status,
        ];
//        dd($update);

        $save = $this->model->update($update);
        if ($save) {
            $arr['status'] = 1;
            $arr['msg'] = '修改成功';
        } else {
            $arr['status'] = 0;
            $arr['msg'] = '修改失败';
        }
        return $arr;
    }

    public function index()
    {
        if(request()->isPost()){
            $map = [];
            $map[] = ['id', 'in', '412, 413'];
            $total = \app\admin\model\Config::where($map)
                ->count();

            $pageIndex = max(input('page'), 1);
            $pageSize = $this->pageSize;
            $p = ($pageIndex - 1) * $pageSize;

            $list = $this->model
                ->where($map)
//                ->order('create_time DESC')
//                ->append(['pay_type_format', 'name_format', 'phone_format'])
                ->limit($p, $pageSize)
                ->select();

            $arr['data'] = $list;
            $arr['code'] = 0;
            // var_dump(1);die;
            $arr['count'] = $total;
            $arr['limit'] = $pageSize;
            $arr['msg'] = "获取" . $this->title . "列表成功";
            return json($arr);
        }else{
            $status = request()->param('status', 'pending');
            View::assign(array(
                'status'        =>    $status,
            ));
            return View::fetch();
        }
    }

    public function setting_shop()
    {
        if(request()->isPost()){
//            dd(request()->param());
            $params = request()->param();
            unset($params['file']);
//            dd($params);
            foreach ($params as $key=>$item)
            {
                if (!empty($item))
                {
                    $this->M->where('key', $key)->update(['value'=>$item]);
                }
            }
//            $update = [
//                'id'       =>  input('id'),
//                'name'     =>  input('name'),
//                'type'     =>  input('type'),
//            ];
//
//            $save = $this->M->update($update);
//            if ($save) {
//                $arr['status'] = 1;
//                $arr['msg'] = '修改成功';
//            } else {
//                $arr['status'] = 0;
//                $arr['msg'] = '修改失败';
//            }
//            return $arr;
        }else{
//            $id = input('id');
            $configList = $this->M
                ->append(['key_format'])
                ->whereIn('id', '215,216,217,218,219')
                ->select();
            View::assign(array(
                'config_list'        =>    $configList,
            ));
            return View::fetch();
        }
    }

    public function edit()
    {
        if(request()->isPost()){
//            dd(request()->param());
            $params = request()->param();
            unset($params['file']);
//            dd($params);
            foreach ($params as $key=>$item)
            {
                if (!empty($item))
                {
                    $this->M->where('key', $key)->update(['value'=>$item]);
                }
            }
//            $update = [
//                'id'       =>  input('id'),
//                'name'     =>  input('name'),
//                'type'     =>  input('type'),
//            ];
//
//            $save = $this->M->update($update);
//            if ($save) {
//                $arr['status'] = 1;
//                $arr['msg'] = '修改成功';
//            } else {
//                $arr['status'] = 0;
//                $arr['msg'] = '修改失败';
//            }
//            return $arr;
        }else{
//            $id = input('id');
            $configList = $this->model
//                ->append(['key_format'])
//                ->whereIn('id', '215,216,217,218,219')
//                ->whereIn('id', '412')
                ->select();
//            dd($configList);
            View::assign(array(
                'config_list'        =>    $configList,
            ));
            return View::fetch();
        }
    }
}